package pingan.com.quarter_hour.acyivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pingan.com.quarter_hour.R;
import pingan.com.quarter_hour.bean.VersionBean;
import pingan.com.quarter_hour.utils.BaseActivity;
import pingan.com.quarter_hour.utils.DataCleanManager;
import pingan.com.quarter_hour.utils.VersionUtil;
import pingan.com.quarter_hour.view.IView;
import pingan.com.quarter_hour.y_interface.HttpIview;
import pingan.com.quarter_hour.y_presenter.HttpPresenter;

public class InstallActivity extends BaseActivity implements HttpIview {

    @BindView(R.id.finish)
    LinearLayout finish;
    @BindView(R.id.textbanben)
    TextView textbanben;
    @BindView(R.id.find_new)
    RelativeLayout findNew;
    @BindView(R.id.textlaji)
    TextView textlaji;
    @BindView(R.id.clean)
    RelativeLayout clean;
    @BindView(R.id.finishuser)
    Button finishuser;
    private String versionName;
    private HttpPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install);
        ButterKnife.bind(this);

        presenter = new HttpPresenter(this);

        //获取现在的版本号
        versionName = VersionUtil.getVersionName(InstallActivity.this);
        textbanben.setText(versionName);

        //获取缓存大小
        try {
            String totalCacheSize = DataCleanManager.getTotalCacheSize(InstallActivity.this);
            textlaji.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @OnClick({R.id.finish, R.id.find_new, R.id.clean,R.id.finishuser})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish:
                finish();
                break;

            //版本更新
            case R.id.find_new:
                HashMap<String, String> map = new HashMap<>();
                presenter.getMap("https://www.zhaoapi.cn/",map,"检测更新");
                break;

            //清理缓存
            case R.id.clean:
                AlertDialog.Builder builder = new AlertDialog.Builder(InstallActivity.this);
                builder.setTitle("清除缓存");
                builder.setMessage("是否清除");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        DataCleanManager.clearAllCache(InstallActivity.this);
                        DataCleanManager.cleanDatabases(InstallActivity.this);
                        Toast.makeText(InstallActivity.this, "清理完成", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(InstallActivity.this, "以后清理", Toast.LENGTH_SHORT).show();
                    }
                });

                try {
                    String totalCacheSize = DataCleanManager.getTotalCacheSize(InstallActivity.this);
                    textlaji.setText(totalCacheSize);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                builder.create().show();
                break;

            case R.id.finishuser:
                Toast.makeText(InstallActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }


    //网络请求成功
    @Override
    public void success(Object o, String tag) {
        if(tag.equals("检测更新")){
            VersionBean versionBean = (VersionBean) o;
            String code = versionBean.getCode();
            if(code.equals("0")){
                AlertDialog.Builder builder1 = new AlertDialog.Builder(InstallActivity.this);
                builder1.setTitle("版本更新");
                builder1.setMessage("检测到新版本（非强制更新）");
                builder1.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(InstallActivity.this, "准备更新", Toast.LENGTH_SHORT).show();
                    }
                });

                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(InstallActivity.this, "以后更新", Toast.LENGTH_SHORT).show();
                    }
                });

                builder1.create().show();
            }
        }

    }

    //防止内存泄漏
    @Override
    protected void onDestroy() {
        super.onDestroy();

        presenter.disAttach();
    }
}
